<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <!--当前示例页面样式表引用-->
    <link rel="stylesheet" href="./static/demo/openlayers/example/style.css" />
    <script include="jquery" src="./static/libs/include-lib-local.js"></script>
    <script src="./static/libs/include-openlayers-local.js"></script>
    <style type="text/css">
        #mapCon {
            width: 100%;
            height: 95%;
            position: absolute;
        }
        /* 图层控件层样式设置 */
        .layerControl {
            position: absolute;
            bottom: 5px;
            min-width: 200px;
            max-height: 102px;
            right: 10px;
            top: 120px;
            /*在地图容器中的层，要设置z-index的值让其显示在地图上层*/
            z-index: 2001;
            color: #ffffff;
            background-color: #4c4e5a;
            /*边缘的宽度*/
            border-width: 10px;
            /*圆角的大小 */
            border-radius: 10px;
            /*边框颜色*/
            border-color: #000 #000 #000 #000;
        }

        .layerTree{
            padding-inline-start: 15px;
        }
        .layerTree li {
            list-style: none;
            margin: 5px 10px;
            font-size: 12px;
        }
        li .layer{
            position: absolute;
            margin-top: 0px;
        }
    </style>
    <script type="text/javascript">
        var map = null;
        var resultBaseUrl = "gdbp://MapGisLocal/OpenLayerVecterMap/sfcls/"; //缓存结果图层的基地址
        var resultLayerArr = new Array();
        function init() {
            //初始化地图容器
            map = new ol.Map({
                target: 'mapCon',     //地图容器div的ID
                controls: ol.control.defaults({
                    attributionOptions: ({
                        collapsible: true
                    })
                }),
                view: new ol.View({
                    center: [30, 28],  //地图初始中心点
                    maxZoom: 28,     //最大瓦片显示级数
                    minZoom: 1,      //最小瓦片显示级数
                    zoom: 3 ,         //地图初始显示级数
                    projection:"EPSG:4326"
                })
            });
            var tdk = "4c27d6e0e8a90715b23a989d42272fd8";   //天地图密钥
            //加载天地图瓦片图层数据
            map.addLayer(new ol.layer.Tile({
                title: "天地图矢量图层",
                source: new ol.source.XYZ({
                    url: "http://t0.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=" + tdk,
                    wrapX: false
                }),
                projection:"EPSG:4326"
            }));
            map.addLayer(new ol.layer.Tile({
                    title: "天地图矢量注记图层",
                    source: new ol.source.XYZ({
                        url: "http://t0.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=" + tdk
                    }),
                    projection:"EPSG:4326"
                })
            );


            var vectorSource = new ol.source.Vector();
            //创建一个多变形
            var polygon = new ol.Feature({
                geometry: new ol.geom.Polygon([[[0.46, 30.1], [11.48, 6.22], [36.73, 7.6],[58.77, 25.51],[41.33, 49.39]]])
            });
            //设置区样式信息
            polygon.setStyle(new ol.style.Style({
                //填充色
                fill: new ol.style.Fill({
                    color: 'rgba(255, 255, 255, 0.5)'
                }),
                //边线颜色
                stroke: new ol.style.Stroke({
                    color: '#ffcc33',
                    width: 2
                })
            }));
            vectorSource.addFeatures([polygon]);

            //创建一个图层
            var vectorLayer = new ol.layer.Vector({
                source: vectorSource,
                zIndex:1
            });
            //将绘制层添加到地图容器中
            map.addLayer(vectorLayer);
            loadLayersControl(map, "layerTree");
        }
        //执行单圈缓冲区分析
        function bufferOneRing()
        {
            startPressBar();
            //初始化Zondy.Object.FeatureGeometry对象
            var regGeo = new Zondy.Object.FeatureGeometry();
            //设置区要素的空间几何信息
            var gReg = new Zondy.Object.GRegion([
                new Zondy.Object.AnyLine([new Zondy.Object.Arc([
                    new Zondy.Object.Point2D(0.46, 30.1),
                    new Zondy.Object.Point2D(11.48, 6.22),
                    new Zondy.Object.Point2D(36.73, 7.6),
                    new Zondy.Object.Point2D(58.77, 25.51),
                    new Zondy.Object.Point2D(41.33, 49.39),
                    new Zondy.Object.Point2D(0.46, 30.1)
                ])
                ])
            ]);
            regGeo.setRegGeom([gReg]);
            //设置属性结构
            var regAttStr = new Zondy.Object.CAttStruct({
                FldName: ["ID", "面积", "周长", "LayerID"],
                FldNumber: 4,
                FldType: ["FldLong", "FldDouble", "FldDouble", "FldLong"]
            });
            //实例化CAttDataRow类
            var values = [0, 62.566714, 50.803211, 0];
            var valuesRow = new Zondy.Object.CAttDataRow(values, 1);
            //实例化FeatureBuffBySingleRing类，设置要素缓冲分析必要参数，输出分析结果到缓冲分析结果图层
            var featureBufBySR = new Zondy.Service.FeatureBuffBySingleRing({
                ip: "develop.smaryun.com",
                port: "6163",    //访问IGServer的端口号，.net版为6163，Java版为8089,
                //设置要素缓冲分析左半径
                leftRad: 2,
                //设置要素缓冲分析右半径
                rightRad: 2
            });
            /*设置缓冲分析参数*/
            //设置几何信息
            featureBufBySR.sfGeometryXML = JSON.stringify([regGeo]);
            //设置属性结构
            featureBufBySR.attStrctXML = JSON.stringify(regAttStr);
            //设置属性值
            featureBufBySR.attRowsXML = JSON.stringify([valuesRow]);
            //设置追踪半径
            featureBufBySR.traceRadius = 0.0001;
            //设置缓冲结果的名称以及存放地址
            var resultname = "singleBuffResultLayer" + getCurentTime();
            featureBufBySR.resultName = resultBaseUrl + resultname;
            //调用Zondy.Service.AnalysisBase基类的execute方法执行要素缓冲分析，AnalysisSuccess为回调函数。
            featureBufBySR.execute(AnalysisSuccess,"post",false,"json",()=>{});
        }

        function bufferMulRings()
        {
            startPressBar();
            //初始化Zondy.Object.FeatureGeometry对象
            var regGeo = new Zondy.Object.FeatureGeometry();
            //设置区要素的空间几何信息
            var gReg = new Zondy.Object.GRegion([
                new Zondy.Object.AnyLine([new Zondy.Object.Arc([
                    new Zondy.Object.Point2D(0.46, 30.1),
                    new Zondy.Object.Point2D(11.48, 6.22),
                    new Zondy.Object.Point2D(36.73, 7.6),
                    new Zondy.Object.Point2D(58.77, 25.51),
                    new Zondy.Object.Point2D(41.33, 49.39),
                    new Zondy.Object.Point2D(0.46, 30.1)
                ])
                ])
            ]);
            //设置区要素几何信息的方法。
            regGeo.setRegGeom([gReg]);
            //实例化CAttStruct类
            var regAttStr = new Zondy.Object.CAttStruct({
                FldName: ["ID", "面积", "周长", "LayerID"],
                FldNumber: 4,
                FldType: ["FldLong", "FldDouble", "FldDouble", "FldLong"]
            });
            var values = [1, 0.00058032464704422, 0.132101984752282, 8];
            //创建属性信息对象
            var valuesRow = new Zondy.Object.CAttDataRow(values, 3286);
            //实例化FeatureBuffByMultiplyRing类，设置要素缓冲分析必要参数，输出分析结果到缓冲分析结果图层
            var featureBufByMR = new Zondy.Service.FeatureBuffByMultiplyRing({
                ip: "develop.smaryun.com",
                port: "6163",    //访问IGServer的端口号，.net版为6163，Java版为8089,
                //设置多圈缓冲分析的缓冲半径字符串
                radiusStr: "2,4,6"
            });
            featureBufByMR.sfGeometryXML = JSON.stringify([regGeo]);
            featureBufByMR.attStrctXML = JSON.stringify(regAttStr);
            featureBufByMR.attRowsXML = JSON.stringify([valuesRow]);
            featureBufByMR.traceRadius = 0.0001;

            var resultname = "multiBuffResultLayer" + getCurentTime();
            featureBufByMR.resultName = resultBaseUrl + resultname;
            //调用Zondy.Service.AnalysisBase基类的execute方法执行要素缓冲分析，AnalysisSuccess为回调函数。
            featureBufByMR.execute(AnalysisSuccess,"post",false,"json", ()=>{});
        }

         //分析成功后的回调
         function AnalysisSuccess(data) {
             //停止进度条
             stopPressBar();
            if (!data.results) {
                alert("缓冲失败，请检查参数！");
            }
            else {
                if (data.results.length != 0) {
                    var resultLayerUrl = data.results[0].Value || data.results[0].value;
                    //将结果图层添加到地图视图中显示
                    var resultLayer = new Zondy.Map.GdbpLayer("MapGIS IGS BuffAnalyResultLayer", [resultLayerUrl], {
                        ip: "develop.smaryun.com",
                        port: "6163",    //访问IGServer的端口号，.net版为6163，Java版为8089,
                        isBaseLayer: false
                    });
                    map.addLayer(resultLayer);
                    resultLayerArr.push(resultLayer);
                }
            }
        }

        function deleteAll()
        {
            while(resultLayerArr.length>0)
            {
                map.removeLayer(resultLayerArr.pop());
            }
        }

        function getCurentTime() {
            var now = new Date();
            //获取当前年份
            var year = now.getFullYear();
            //获取当前月份
            var month = now.getMonth() + 1;
            //获取当前日期
            var day = now.getDate();
            //获取当前时刻
            var hh = now.getHours();
            //获取当前分钟
            var mm = now.getMinutes();
            //获取当前秒钟
            var ss = now.getSeconds();
            //将当前的日期拼串
            var clock = year + "-";
            if (month < 10)
                clock += "0";
            clock += month + "-";
            if (day < 10)
                clock += "0";
            clock += day + "-";
            if (hh < 10)
                clock += "0";
            clock += hh;
            if (mm < 10) clock += '0';
            clock += mm;
            if (ss < 10) clock += '0';
            clock += ss;
            return (clock);
        }
        //map中的图层数组
        var layerArr = new Array();
        //图层名称数组
        var layerNameArr = new Array('天地图矢量图层','天地图矢量注记图层','缓冲要素');
        //图层可见属性数组
        var layerVisibilityArr = new Array();

        /**
         * 加载图层列表数据
         * @param {ol.Map} map 地图对象
         * @param {string} id 图层列表容器ID
         */
        function loadLayersControl(map, id) {
            //图层目录容器
            var treeContent = document.getElementById(id);
            //获取地图中所有图层
            var layers = map.getLayers();
            for (var i = 0; i < layers.getLength() ; i++) {
                //获取每个图层的名称、是否可见属性
                layerArr[i] = layers.item(i);
                // layerNameArr[i] = layerArr[i].get('name');
                layerVisibilityArr[i] = layerArr[i].getVisible();
                //新增li元素，用来承载图层项
                var elementLi = document.createElement('li');
                // 添加子节点
                treeContent.appendChild(elementLi);
                //创建复选框元素
                var elementInput = document.createElement('input');
                elementInput.type = "checkbox";
                elementInput.name = "layers";
                elementLi.appendChild(elementInput);
                //创建label元素
                var elementLable = document.createElement('label');
                elementLable.className = "layer";
                //设置图层名称
                setInnerText(elementLable, layerNameArr[i]);
                elementLi.appendChild(elementLable);
                //设置图层默认显示状态
                if (layerVisibilityArr[i]) {
                    elementInput.checked = true;
                }
                //为checkbox添加变更事件
                addChangeEvent(elementInput, layerArr[i]);
            }
        }
        /**
         * 为checkbox元素绑定变更事件
         * @param {input} element checkbox元素
         * @param {ol.layer.Layer} layer 图层对象
         */
        function addChangeEvent(element, layer) {
            element.onclick = function () {
                if (element.checked) {
                    //显示图层
                    layer.setVisible(true);
                }
                else {
                    //不显示图层
                    layer.setVisible(false);
                }
            };
        }
        /**
         * 动态设置元素文本内容（兼容）
         */
        function setInnerText(element, text) {
            if (typeof element.textContent == "string") {
                element.textContent = text;
            } else {
                element.innerText = text;
            }
        }

        /*===========================================进度条===================================================*/
        //停止进度条
        function stopPressBar() {
            document.getElementById("preview").style.display = "none";
        }
        //开始进度条动画
        function startPressBar() {
            document.getElementById("preview").style.display = "block";
        }
    </script>
</head>

<body onload="init()">
<div id="mapCon">
    <div id="layerControl" class="layerControl">
        <ul id="layerTree" class="layerTree"></ul>
    </div>
</div>


    <div id="menuContain" class='menuContain'>
        <div id="tool-container">
         <div id='dataSourceMenuID'  class="optmain"  status="unactive" onclick="switchMenuStatus(this,'menu1')">
             <span></span><i class="menuGroup">要素缓冲</i><em></em>
         </div>
      
     </div>
 </div>
 <div id="menu1" class="menuStrip" style='display:none' >
     <ul  class="menuItems">
         <li   onclick="bufferOneRing()"><span class="item1"></span><i >单圈</i>
         </li>
         <li   onclick="bufferMulRings()"><span class="item1"></span><i >多圈</i>
         </li>
         <li class="divider"></li>
         <li  onclick="deleteAll()"><span class="item3"></span><i>清除</i>
         </li>
     </ul>
  </div>
    <div id="preview">
        <img src="static/assets/graphic-image/39-1.gif" alt='' /><br />
        <br />
        <span>正在分析，请稍候</span>
    </div>
 <script>

    function switchMenuStatus(div, menuitemFrameID) {
        var temDivs = document.getElementsByClassName('optmain');
        if (temDivs.length > 0) {
            for (var i = 0; i < temDivs.length; i++) {
                if (temDivs[i] === div) {
                    var status = div.getAttribute("status");
                    if (status == "unactive") {
                        div.setAttribute("status", "active");
                        var tem_spans = div.getElementsByTagName("span");
                        var tem_ems = div.getElementsByTagName("em");
                        tem_spans[0].className = "active";
                        tem_ems[0].className = "active";

                        //显示菜单项
                        DisplayMenuItem(true, menuitemFrameID);

                    }
                    else {
                        div.setAttribute("status", "unactive");
                        var tem_spans = div.getElementsByTagName("span");
                        var tem_ems = div.getElementsByTagName("em");
                        tem_spans[0].className = "";
                        tem_ems[0].className = "";

                        //隐藏菜单项
                        DisplayMenuItem(false, menuitemFrameID);
                    }
                }
                else {
                    var status = temDivs[i].getAttribute("status");
                    if (status == "active") {
                        temDivs[i].setAttribute("status", "unactive");
                        var tem_spans = temDivs[i].getElementsByTagName("span");
                        var tem_ems = temDivs[i].getElementsByTagName("em");
                        tem_spans[0].className = "";
                        tem_ems[0].className = "";
                    }
                }
            }
        }
    }

    function DisplayMenuItem (isDisplay, iframeID) {
        var menuItemFrame = document.getElementById(iframeID);
        if (menuItemFrame != null) {
            if (isDisplay) {
                var temDivs = document.getElementsByClassName('menuStrip');
                if (temDivs.length > 0) {
                    for (var i = 0; i < temDivs.length; i++) {
                        if (temDivs[i] != menuItemFrame) {
                            temDivs[i].style.display = "none";
                        }
                    }
                }
                menuItemFrame.style.display = "";
            }
            else {
                menuItemFrame.style.display = "none";
            }
        }
    };
</script>
</body>

</html>